<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Auth;

class AdminerRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $this->confirmPwd();
        return [
            'password' => 'sometimes|required|confirmed',
            'password_confirmation' => 'sometimes|required',
            'old_pwd' => 'sometimes|required|check_pwd',
        ];
    }

    public function messages()
    {
        return [
            'old_pwd.required' => '旧密码不允许为空',
            'password.required' => '新密码不允许为空',
            'password_confirmation.required' => '确认密码不允许为空',
            'password.confirmed' => '输入密码不一致',
            'old_pwd.check_pwd' => '旧密码错误',
        ];
    }

    public function confirmPwd()
    {
        //dd(Auth::guard('admin')->user());
        Validator::extend('check_pwd', function ($attribute, $value, $parameters, $validator) {
            return Hash::check($value,Auth::guard('admin')->user()->password);
        });
    }

}

